import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class PrintICS {
	private String version;
	private String classification;
	private String classification_end;
	private String comment;
	private String location;
	private String resources;
	private String dTSTART;
	private String dTEND;
	private String timeZone;
	private String recurring;
	private String attachment;
	private String reminder;


	public PrintICS(){
		version="";
		classification="";
		comment="";
		location="";
		resources="";
		dTSTART="";
		dTEND="";
		timeZone="";
		recurring="";
		attachment = "";
		reminder = "";
	}

	public void setReminder(String s, String r) {	
		int day = 0;
		
		if (r == "Minute") 
			reminder = "-P0DT0H" + s + "M0S";
		if (r == "Hour")
			reminder = "-P0DT" + s + "H0M0S";
		if (r == "Day")
			reminder = "-P" + s + "D";
		if (r == "Week") {
			day = Integer.parseInt(s);
		    day = day*7;
			reminder = "-P" + day + "D";
		}
	}
	
	public void setAttachment(String s) {
		attachment = s;
	}


	/************************************
	 * Parameter:  "2.0";
	 ************************************/
	public void setVersion(String vervalue) {
		version = vervalue;
	}



	/******************************************************************
	 * Parameter:  The property can be specified once in a "VEVENT",
	 * "VTODO", or "VJOURNAL" calendar components.
	 *****************************************************************/
	public void setClassification(String s){

		classification= s;
		classification_end= s;

	}


	/*********************************
	 * Parameter:  text
	 ********************************/
	public void setComment(String s){
		comment = s;
	}


	/**************************
	 * Parameter:  text
	 **************************/
	public void setLocation(String s){
		location = s;
	}

	/**************************
	 * Parameter:  text
	 **************************/
	public void setResources(String s){
		resources = s;
	}

	/******************************
	 * Parameter:  19960401T150000Z
	 * or DTEND;VALUE=DATE:19980704
	 ******************************/
	public void setDTSTART(String s){
		dTSTART = s;
	}

	/******************************
	 * Parameter:  19980118T073000Z
	 ******************************/
	public void setDTEND(String s){
		dTEND = s;
	}


	/*********************************
	 * Parameter:  America/New_York
	 * Parameter:  America/Los_Angeles
	 ********************************/
	public void setTimeZone(String s){
		timeZone = s;
	}

	/*************************************
	 * Parameter: (RRULE:INTERVAL=?; FREQ=?
	 *************************************/
	public void setRecurring(String s){
		recurring = s;
	}


	public void print(){
		try {
          String outputString = "BEGIN:VCALENDAR"        
          + "\r\nVERSION:"           + version   
          + "\r\nPRODID:-//ICS414Project version 4.0"
          + "\r\nCALSCALE:GREGORIAN" 
          + "\r\nTZID:"              + timeZone
          + "\r\nBEGIN:"             + classification
          + "\r\nDTSTART:"           + dTSTART
          + "\r\nATTACH:"            + attachment
          + "\r\nCOMMENT:"           + comment
          + "\r\nLOCATION:"          + location
          + "\r\nRESOURCES:"         + resources
          + (recurring.compareTo("") == 0 ? "" : "\r\n" + recurring)
          + "\r\nTRIGGER:"           + reminder
          + "\r\nDTEND:"             + dTEND
          + "\r\nEND:"               + classification_end
          + "\r\nEND:VCALENDAR";
          
			System.out.println("Creating file event.ics.");
			FileWriter fw = new FileWriter(new File("event.ics"));
			fw.write(outputString);
			fw.close();
			System.out.println("Done.\n");

			System.out.println(outputString);
		} catch (IOException e) {
			System.err.println("An error (" + e + ") occurred when opening the file. Aborting.");
			return;
		}
	}

	/*
	 * Block of get methods used for testing
	 */
	public String getVersion() {
		return "VERSION:" + version;
	}

	public String getClassification() {
		return "BEGIN:" + classification;
	}

	public String getClassificationEnd() {
		return "END:" + classification_end;
	}

	public String getComment() {
		return "COMMENT:" + comment;
	}

	public String getLocation() {
		return "LOCATION:" + location;
	}

	public String getResources() {
		return "RESOURCES:" + resources;
	}

	public String getDTSTART() {
		return "DTSTART:" + dTSTART;
	}

	public String getDTEND() {
		return "DTEND:" + dTEND;
	}

	public String getTimeZone() {
		return "TZID:" + timeZone;
	}

	public String getRecurring() {
		return recurring;
	}
	
	public String getAttachment() {
		return "ATTACH:" + attachment;
	}
	
	public String getReminder() {
		return "TRIGGER" + reminder;
	}
}
